package com.jingdun.sport.api.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jingdun.sport.bean.model.*;
import com.jingdun.sport.common.util.DateUtil;
import com.jingdun.sport.service.RoomService;
import com.jingdun.sport.service.SportRecordService;
import com.jingdun.sport.service.TzUserFanService;
import com.jingdun.sport.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/sport/record/")
@Api(tags = "记录接口")
public class NewRecordController {

    @Autowired
    private SportRecordService sportRecordService;

    @Autowired
    private RoomService roomService;

    @Autowired
    private UserService userService;

    @Autowired
    TzUserFanService tzUserFanService;

    /**
     * 获取记录列表
     */
    @GetMapping("/getFinishedRecordList")
    @ApiOperation(value = "获取记录列表", notes = "获取记录列表")
    public ApiResult getRecordList(Long sportTypeId, String userId) {
        List<SportRecord> oldSportRecordList = sportRecordService.getListTop10(sportTypeId);
        Map<String, SportRecord> sportRecordMap = new HashMap<>();
        for (SportRecord oldSportRecord : oldSportRecordList) {
            if (sportRecordMap.size() < 11) {
                if (sportRecordMap.keySet() != null && sportRecordMap.keySet().contains(oldSportRecord.getUser())) {
                    SportRecord sportRecord = sportRecordMap.get(oldSportRecord.getUser());
                    if (DateUtil.compareDate(oldSportRecord.getEnd(), sportRecord.getEnd())) {
                        sportRecordMap.put(oldSportRecord.getUser(), oldSportRecord);
                    }
                } else {
                    sportRecordMap.put(oldSportRecord.getUser(), oldSportRecord);
                }
            } else {
                break;
            }
        }

        List<SportRecord> sportRecordList = new ArrayList<>(sportRecordMap.values());
        ApiResult apiResult = new ApiResult();
        if (!sportRecordList.isEmpty()) {
            Set<Long> activityIdSet = sportRecordList.stream().map(SportRecord::getActivity).collect(Collectors.toSet());
            List<Room> roomList = roomService.getRoomListByActivityId(activityIdSet);
            Map<Long, Long> roomId2ActivityIdMap = roomList.stream().collect(Collectors.toMap(Room::getActivityId, Room::getId));
            Set<String> userIdSet = sportRecordList.stream().map(SportRecord::getUser).collect(Collectors.toSet());
            Collection<User> users = userService.listByIds(userIdSet);
            List<User> userList = new ArrayList<>(users);
            Map<String, User> userId2UserMap = userList.stream().collect(Collectors.toMap(User::getUserId, User -> User));
            List<SportRecordDto> resultSportRecord = new ArrayList<>(sportRecordList.size());
            List<SportRecordDto> finalResultSportRecord = resultSportRecord;
            sportRecordList.stream().forEach(sportRecord -> {
                SportRecordDto sportRecordDto = new SportRecordDto();
                sportRecordDto.setGmtCreate(sportRecord.getEnd().toString());
                sportRecordDto.setId(sportRecord.getId());
                sportRecordDto.setLogo(userId2UserMap.get(sportRecord.getUser()).getPic());
                sportRecordDto.setNickname(userId2UserMap.get(sportRecord.getUser()).getNickName());
//                sportRecordDto.setResult(DateUtil.formatDateTimeToStr(sportRecord.getEnd()));
                Map<String, Integer> r = new HashMap<>(2);
                r.put("type", sportRecord.getType());
                r.put("count", sportRecord.getMeasure().intValue());
                sportRecordDto.setResult(r);
                sportRecordDto.setRoomId(roomId2ActivityIdMap.get(sportRecord.getActivity()));
                sportRecordDto.setUserId(sportRecord.getUser());
                QueryWrapper<TzUserFan> tzUserFanQueryWrapper = new QueryWrapper<>();
                tzUserFanQueryWrapper.eq("user_id", userId).eq("be_fan_user_id", sportRecord.getUser()).eq("enable", "1");
                int count = tzUserFanService.count(tzUserFanQueryWrapper);
                sportRecordDto.setIsFollow(count);
                finalResultSportRecord.add(sportRecordDto);
            });
            resultSportRecord.sort((t1, t2) -> new Date(t2.getGmtCreate()).compareTo(new Date(t1.getGmtCreate())));
            apiResult.setData(resultSportRecord);
        }
        return apiResult;
    }
}